package com.liuda066.forumssm.services.impl;

import com.liuda066.forumssm.common.AppResult;
import com.liuda066.forumssm.common.ResultCode;
import com.liuda066.forumssm.dao.BoardMapper;
import com.liuda066.forumssm.exception.ApplicationException;
import com.liuda066.forumssm.model.Board;
import com.liuda066.forumssm.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/9/4 20:28
 */
@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;
    @Override
    public void create(Board board) {
        if (board == null){
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //设备初值
        board.setArticleCount(0);
        board.setState((byte) 0);
        board.setDeleteState((byte) 0);
        Date date = new Date();
        board.setCreateTime(date);
        board.setUpdateTime(date);
        int row = boardMapper.insertSelective(board);
        if (row != 1){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        log.info("新增板块成功,board = "+board.toString());
    }

    @Override
    public Board selectById(Long id) {
        if (id == null || id <= 0){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Board> selectByNum(Integer num) {
        if (num <= 0){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;
    }

    @Override
    public List<Board> selectAllNormal() {
        return boardMapper.selectAllNormal();
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if (id == null || id<=0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        Board updateBoard= new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);

        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1){
            log.warn(ResultCode.FAILED.toString()+"受影响的行数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public void subOneArticleCountById(Long id) {
        if (id == null || id<=0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        Board updateBoard= new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if (updateBoard.getArticleCount() < 0){
            updateBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1){
            log.warn(ResultCode.FAILED.toString()+"受影响的行数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
